﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2QWebProxy.App_Code;

namespace _2QWebProxy.GUI
{
    public partial class ucPrivacyFileTypes : UserControl
    {

        public bool isDenyForm = false;

        public ucPrivacyFileTypes(bool isDenyForm)
        {
            this.isDenyForm = isDenyForm;
            InitializeComponent();
            LoadListCommonFileTypes();
            LoadPrivacyFile();
        }

        private void LoadListCommonFileTypes()
        {
            cbbFileTypes.DataSource = Utils.GetCommonFileType();
        }

        private void LoadPrivacyFile()
        {
            if (isDenyForm)
            {
                dgvPrivacyFileType.DataSource = Utils.GetPrivacyFile("deny");
            }
            else
            {
                dgvPrivacyFileType.DataSource = Utils.GetPrivacyFile("allow");
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            PrivacyFile file = new PrivacyFile()
            {
                Type = cbbFileTypes.SelectedValue.ToString()
            };
            Utils.DeletePrivacyFile(file);
            LoadPrivacyFile();
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            Utils.DeleteAllPrivacyFile();
            LoadPrivacyFile();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        private void dgvPrivacyFileType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvPrivacyFileType.SelectedCells[0].RowIndex;
            cbbFileTypes.SelectedIndex = cbbFileTypes.FindStringExact(dgvPrivacyFileType.Rows[index].Cells[0].Value.ToString());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (isDenyForm)
            {
                PrivacyFile file = new PrivacyFile()
                {
                    Type = cbbFileTypes.SelectedValue.ToString(),
                    Status = "deny"
                };
                Utils.SavePrivacyFile(file);
            }
            else
            {
                PrivacyFile file = new PrivacyFile()
                {
                    Type = cbbFileTypes.SelectedValue.ToString(),
                    Status = "allow"
                };
                Utils.SavePrivacyFile(file);
            }

            LoadPrivacyFile();
        }
    }
}
